/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.longview.io.modules.livestockinfo.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.longview.io.common.config.Global;
import com.longview.io.common.persistence.Page;
import com.longview.io.common.web.BaseController;
import com.longview.io.common.utils.DateUtils;
import com.longview.io.common.utils.StringUtils;
import com.longview.io.common.utils.excel.ExportExcel;
import com.longview.io.modules.livestockinfo.entity.UserLivestockInfo;
import com.longview.io.modules.livestockinfo.entity.UserLivestockInfoExp;
import com.longview.io.modules.livestockinfo.service.UserLivestockInfoService;
import com.longview.io.modules.sys.entity.Dict;
import com.longview.io.modules.sys.utils.DictUtils;
import com.longview.io.modules.userinfo.entity.UserInfo;
import com.longview.io.modules.userinfo.entity.UserInfoExp;

/**
 * 用户牲畜信息Controller
 * 
 * @author zf
 * @version 2018-04-23
 */
@Controller
@RequestMapping(value = "${adminPath}/livestockinfo/userLivestockInfo")
public class UserLivestockInfoController extends BaseController {

	@Autowired
	private UserLivestockInfoService userLivestockInfoService;

	@ModelAttribute
	public UserLivestockInfo get(@RequestParam(required = false) String id) {
		UserLivestockInfo entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = userLivestockInfoService.get(id);
		}
		if (entity == null) {
			entity = new UserLivestockInfo();
		}
		return entity;
	}

	@RequiresPermissions("livestockinfo:userLivestockInfo:view")
	@RequestMapping(value = { "list", "" })
	public String list(UserLivestockInfo userLivestockInfo, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<UserLivestockInfo> page = userLivestockInfoService.findPage(new Page<UserLivestockInfo>(request, response),
				userLivestockInfo);
		// 获取所有动物信息
		List<UserLivestockInfo> liststock = userLivestockInfoService.findSumList(userLivestockInfo);
		// 查询
		// List<UserLivestockInfo> list =
		// userLivestockInfoService.findList(userLivestockInfo);
		String sumMessage = "总计:";
		List<Dict> stocklist = DictUtils.getDictList("livestock_type");
		for (int i = 0; i < stocklist.size(); i++) {
			Dict dict = stocklist.get(i);
			for (int z = 0; z < liststock.size(); z++) {
				UserLivestockInfo livestock = liststock.get(z);
				if (dict.getValue().compareTo(livestock.getLivestockType()) == 0) {
					sumMessage += dict.getLabel() + ":" + livestock.getSumCount() + "只,";
				}
			}
		}
		model.addAttribute("stocklist", stocklist);
		model.addAttribute("page", page);
		model.addAttribute("message", sumMessage);
		return "modules/livestockinfo/userLivestockInfoList";
	}

	/**
	 * 导出用户数据
	 * 
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("livestockinfo:userLivestockInfo:view")
	@RequestMapping(value = "export", method = RequestMethod.POST)
	public String exportFile(UserLivestockInfo userLivestockInfo, HttpServletRequest request,
			HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "牲畜数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
			Page<UserLivestockInfo> page = userLivestockInfoService.findPagepdtList(new Page<UserLivestockInfo>(request, response), userLivestockInfo);
			
			new ExportExcel("牲畜数据", UserLivestockInfoExp.class).setDataList(page.getList()).write(response, fileName).dispose();		 
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出牲畜失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + adminPath + "/livestockinfo/userLivestockInfo/?repage";
	}

	@RequiresPermissions("livestockinfo:userLivestockInfo:view")
	@RequestMapping(value = "form")
	public String form(UserLivestockInfo userLivestockInfo, Model model) {
		model.addAttribute("userLivestockInfo", userLivestockInfo);
		return "modules/livestockinfo/userLivestockInfoForm";
	}

	@RequiresPermissions("livestockinfo:userLivestockInfo:edit")
	@RequestMapping(value = "save")
	public String save(UserLivestockInfo userLivestockInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, userLivestockInfo)) {
			return form(userLivestockInfo, model);
		}
		userLivestockInfoService.save(userLivestockInfo);
		addMessage(redirectAttributes, "保存用户牲畜信息成功");
		return "redirect:" + Global.getAdminPath() + "/livestockinfo/userLivestockInfo/?repage";
	}

	@RequiresPermissions("livestockinfo:userLivestockInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(UserLivestockInfo userLivestockInfo, RedirectAttributes redirectAttributes) {
		userLivestockInfoService.delete(userLivestockInfo);
		addMessage(redirectAttributes, "删除用户牲畜信息成功");
		return "redirect:" + Global.getAdminPath() + "/livestockinfo/userLivestockInfo/?repage";
	}

}